package com.bee.web.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bee.web.annotation.ValidateToken;
import com.bee.web.service.UserService;
import com.bee.web.util.FastJsonUtil;
import com.bee.web.util.StringUtil;
import com.bee.web.vo.BaseVo;
import com.bee.web.vo.UserLoginVo;
import com.bee.web.vo.bean.UserInfoBean;
import com.bee.web.vo.bean.UserLoginBean;

/**
 * 用户接口
 *
 * @author liuzy
 * @version 2015年6月12日
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    private Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo login(@RequestBody(required = false) UserLoginBean bean) {
        logger.info("[参数]" + FastJsonUtil.toJSON(bean));
        if (bean == null) {
            return new BaseVo("参数错误");
        }
        try {
            String userId = userService.getUserIdByUserName(bean.getUsername());
            if (StringUtil.isNullOrEmpty(userId)) {
                return new BaseVo("用户不存在");
            }
            if (!userService.login(bean.getUsername(), bean.getPassword())) {
                return new BaseVo("密码错误");
            }
            UserLoginVo vo = new UserLoginVo();
            vo.setToken(tokenManager.createToken(userId));
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseVo("error", "服务器异常");
        }
    }

    @ValidateToken
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo logout(@RequestParam(required = false) String sid) {
        logger.info("[参数]" + FastJsonUtil.toJSON(sid));
    	try {
    		tokenManager.removeToken(sid);
    		BaseVo vo = new BaseVo("yes", "注销成功");
            logger.info("[返回]" + FastJsonUtil.toJSON(vo));
            return vo;
		} catch (Exception e) {
			e.printStackTrace();
            return new BaseVo("error", "服务器异常");
		}
    }
    
    @ValidateToken
    @RequestMapping(value = "/upassword", method = RequestMethod.POST)
    @ResponseBody
    public BaseVo upassword(@RequestBody(required = false) UserInfoBean bean) {
    	logger.info("[参数]" + FastJsonUtil.toJSON(bean));
        if (bean == null) {
            return new BaseVo("参数错误");
        }
        try {
        	BaseVo vo = null;
            String userId = userService.getUserIdByUserName(bean.getUsername());
            if (StringUtil.isNullOrEmpty(userId)) {
                vo = new BaseVo("用户不存在");
            } else if (!userService.login(bean.getUsername(), bean.getPassword())) {
                vo = new BaseVo("旧密码错误");
            } else {
            	if (userService.uPassword(userId, bean.getRepassword())) {
					vo = new BaseVo("success", "修改密码成功");
				} else {
					vo = new BaseVo("修改密码失败");
				}
            }
    		logger.info("[返回]" + FastJsonUtil.toJSON(vo));
    		return vo;
    	} catch (Exception e) {
    		e.printStackTrace();
    		return new BaseVo("error", "服务器异常");
    	}
    }
    
}
